#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QMainWindow *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);
  QToolButton* buttonStartPresentation = new QToolButton(this);
  QToolButton* buttonQuitProgram = new QToolButton(this);
  QToolButton* buttonHelpProgram = new QToolButton(this);
  QMenu* trayIconMenu = new QMenu(this);
  QSystemTrayIcon* SystemTrayIcon = new QSystemTrayIcon(this);

  pBar = new progressbar(this);
  ptmWidget = new PTMWidget (this);

  QAction* actionShowMainWindow = new QAction(tr("Show Main Window"), this);
  QAction* actionHideMainWindow = new QAction(tr("Hide Main Window"), this);

  ui->verticalLayout->layout()->addWidget(ptmWidget);
  ui->horizontalLayout->layout()->addWidget(buttonStartPresentation);
  ui->horizontalLayout->layout()->addWidget(buttonHelpProgram);
  ui->horizontalLayout->addStretch();
  ui->horizontalLayout->layout()->addWidget(buttonQuitProgram);

  SystemTrayIcon->setIcon(QIcon(":/images/samba.png"));
  buttonStartPresentation->setIcon(QIcon(":/images/runit.png"));
  buttonHelpProgram->setIcon(QIcon(":/images/help_index.png"));
  buttonQuitProgram->setIcon(QIcon(":/images/exit.png"));

  buttonQuitProgram->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  buttonHelpProgram->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
  buttonStartPresentation->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

  buttonStartPresentation->setText("Run");
  buttonHelpProgram->setText("Help");
  buttonQuitProgram->setText("Exit");
  trayIconMenu->addAction(actionShowMainWindow);
  trayIconMenu->addAction(actionHideMainWindow);
  trayIconMenu->addAction(ui->actionAbout_program);
  trayIconMenu->addSeparator();
  trayIconMenu->addAction(ui->actionQuit);
  SystemTrayIcon->setContextMenu(trayIconMenu);

  SystemTrayIcon->show();

  connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(closeProgram())); //quit action-button for close program
  connect(buttonQuitProgram, SIGNAL(clicked()),this, SLOT(closeProgram())); //quit button for close program

  connect(actionHideMainWindow, SIGNAL(triggered()), this, SLOT(hide())); //hide mainwindow
  connect(actionShowMainWindow, SIGNAL(triggered()), this, SLOT(showNormal())); //show window

  connect(SystemTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
          this, SLOT(clickTrayIcon(QSystemTrayIcon::ActivationReason)));        //Get click in the tray
  connect(this, SIGNAL(mouseClickedOnTray(bool)), this, SLOT(setVisible(bool))); //set task for click on the tray

  connect(buttonHelpProgram, SIGNAL(clicked()), qApp, SLOT(aboutQt())); //TEMP connection for helpButton

  connect (buttonStartPresentation, SIGNAL (clicked ()), this, SLOT (startPresentation ()));

  connect(pBar, SIGNAL(clickedCloseButton()), this, SLOT(closedProgressBar()));
  connect(pBar, SIGNAL(timerIsOver()), this, SLOT(clickedNextAtProgressBar()));
  connect(pBar, SIGNAL(clickedNextButton()), this, SLOT(clickedNextAtProgressBar()));
  connect(pBar, SIGNAL(clickedIncrementButton()), this, SLOT(clickedIncAtProgressBar()));

  connect(ptmWidget,SIGNAL(cellDoubleClicked(int,int)), this, SLOT(clickedOnTableWidget(int,int)));

  this->Run();
}

MainWindow::~MainWindow()
{
  delete ptmWidget;
  delete pBar;
  delete ui;
}

void MainWindow::startPresentation (void) {
    ptmWidget->setEnabledRows (0, ptmWidget->rowCount () - 1);
    ptmWidget->selectRow (0);
    clickedOnTableWidget (0, 0);
}

void MainWindow::OpenFile(void)
{
  if(!ptmWidget->loadFromXML(QFileDialog::getOpenFileName(this,tr("Open configuration file"), "",
                                             tr("Configuration Xml file (*.xml)"))))
   {
      QMessageBox msgBox;
      msgBox.setText("Sorry, current xml file is corrupted.");
      msgBox.setInformativeText("Do you want open another xml file?");
      msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
      msgBox.setDefaultButton(QMessageBox::Yes);
      int ret = msgBox.exec();
      if (ret==QMessageBox::Yes)
        {
          this->OpenFile();
        }

    }
}


void MainWindow::clickTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
  switch (reason) {
  case QSystemTrayIcon::Trigger:
  case QSystemTrayIcon::DoubleClick:
      this->mouseClickedOnTray(!this->isVisible());
      break;
  case QSystemTrayIcon::MiddleClick:
      qDebug()<<"Midle click";
      break;
  default:
      break;
  }

}

void MainWindow::closeEvent(QCloseEvent *event)
{
      this->hide();
      event->ignore();
}

void MainWindow::closeProgram(void)
{
  QMessageBox msgBox;
  msgBox.setText("Exit from program?!");
  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  msgBox.setDefaultButton(QMessageBox::No);
  int ret = msgBox.exec();
  if (ret==QMessageBox::Yes)
    {
      qApp->quit();
    }

}

void MainWindow::Run(void)
{
  if(!ptmWidget->loadFromXML())
  {
    QMessageBox msgBox;
    msgBox.setText("Sorry, but config-xml not found in the default place.");
    msgBox.setInformativeText("Do you want open config-xml with default files explorer?");
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::Yes);
    int ret = msgBox.exec();
    if (ret==QMessageBox::Yes)
      {
        this->OpenFile();
      }
  }
}

void MainWindow::closedProgressBar(void)
{
    unsigned int currentRow = ptmWidget->currentRow ();
    ptmWidget->setDisabledRows (currentRow, currentRow);
    this->show();
}

void MainWindow::clickedIncAtProgressBar(void)
{
    ptmWidget->incEventDuration (ptmWidget->currentRow ());
}

void MainWindow::clickedNextAtProgressBar(void)
{
    unsigned int nextRow = (unsigned int) ptmWidget->currentRow () + 1;
    if (nextRow < (unsigned int) ptmWidget->rowCount ()) {
        ptmWidget->selectRow (nextRow);
        clickedOnTableWidget (nextRow, 0);
    }
    else {
        ptmWidget->setDisabledRows (0, nextRow - 1);
        pBar->hide();
        this->show();
    }
}

void MainWindow::changeSettings()
{

}

void MainWindow::clickedOnTableWidget(int row, int)
{
    ptmWidget->setDisabledRows (0, row - 1);
    ptmWidget->baseTimeToCurrentTime (row);
    unsigned int duration = ptmWidget->item (row, 1)->text ().toInt ();
    QString eventType = ptmWidget->verticalHeaderItem (row)->text ();
    QString filePath = ptmWidget->item (row, 4)->text ();
    this->hide ();
    pBar->startWork (duration ,eventType, filePath);
}
